<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">

          <el-form-item label="" prop="channel">
            <el-select
              v-model="queryParams.channel"
              placeholder="渠道"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in channelList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="twoExamineBy">
            <el-select
              v-model="queryParams.twoExamineBy"
              placeholder="审核人员"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in userList"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="userPhone">
            <el-input
              v-model="queryParams.userPhone"
              :placeholder="$t('pleaseEnterYourMobileNumber')"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="" prop="userName">
            <el-input
              v-model="queryParams.userName"
              :placeholder="$t('enterOneUserName')"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="" prop="orderSn">
            <el-input
              v-model="queryParams.orderSn"
              :placeholder="$t('pleaseEnterTheOrderNumber')"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="" prop="isFollowUp">
            <el-select
              v-model="queryParams.isFollowUp"
              placeholder="是否跟进"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in isFollowUpList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="channel" v-if="searchShow">
            <el-select
              v-model="queryParams.channel"
              placeholder="渠道"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in channelList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="firstApply">
            <el-select
              v-model="queryParams.firstApply"
              placeholder="贷款状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in applyStatusList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="applyLog">
            <el-select
              v-model="queryParams.applyLog"
              placeholder="是否有放款记录"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in applyLogList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="" prop="overStatus">
            <el-select
              v-model="queryParams.overStatus"
              placeholder="是否逾期"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in overStatusList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              :start-placeholder="$t('distributionStartTime')"
              :end-placeholder="$t('distributionEndTime')"
            ></el-date-picker>
          </el-form-item>


          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
                $t('search')
              }}
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('reset') }}</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              :disabled="multiple"
              @click="handleUpdateIds"
            >批量拒绝
            </el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="id"/>
          <el-table-column :label="$t('platform')" align="center" prop="companyId"/>
          <el-table-column label="渠道" align="center" prop="channelName"/>
          <el-table-column :label="$t('orderNumber')" align="center" prop="orderSn"/>
          <el-table-column :label="$t('userName')" align="center" prop="userName"/>
          <el-table-column :label="$t('phoneNumber')" align="center" prop="userPhone"/>
          <el-table-column label="审核人" align="center" prop="twoExamineBy">
            <template slot-scope="scope">
              <span v-for="item in userList" v-if="scope.row.twoExamineBy == item.userId">{{ item.userName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="贷款状态" align="center" prop="firstApply">
            <template slot-scope="scope">
              <span v-if="scope.row.firstApply == 0">
                首借
              </span>
              <span v-else>复借</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('status')" align="center" prop="status">
            <template slot-scope="scope">
              <span v-if="scope.row.status == '2'">{{ $t('orderStatus.pendingFinalJudgment') }}</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('timeOfApplication')" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('allocateTime')" align="center" prop="twoExamineTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.twoExamineTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="跟进备注" align="center" prop="followUpRemark" width="160"/>
          <el-table-column label="跟进时间" align="center" prop="followUpTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.followUpTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('operate')"
            align="center"
            width="160"
            fixed="right"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
              >
                <router-link
                  :to="{path:'/app/user-info/index/' + scope.row.id + '/' + scope.row.userId, query:queryParams}"
                  class="link-type">
                  <span>{{ $t('see') }}</span>
                </router-link>
              </el-button>
              <!--<el-button-->
              <!--size="mini"-->
              <!--type="text"-->
              <!--icon="el-icon-delete"-->
              <!--@click="handleExamine(scope.row)"-->
              <!--&gt;审核-->
              <!--</el-button>-->
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >{{ $t("close") }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 分配 -->
    <el-dialog title="拒绝" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item :label="$t('frozen')" prop="frozenDay">
          <el-input v-model="form.frozenDay" onkeyup="this.value=this.value.replace(/\D/g,'')">
            <template slot="append">{{ $t('day') }}</template>
          </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="3" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="examineFailHandler">拒 绝</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {examineTwoApply, closeApply, listTwoApply, listExamineUser} from "@/api/app/apply";
import {listProduct} from "@/api/app/product";
import {channelList} from "@/api/app/channel";

export default {
  name: "TwoIndex",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      form1Show: false,
      searchShow: this.$store.state.user.companyId ? false : true,
      // 总条数
      total: 0,
      // 订单表格数据
      applyList: null,
      applyProductList: null,
      // 公司数据
      companyList: null,
      channelList: null,
      // 审核人员数据
      userList: null,
      companyId: this.$store.state.user.companyId,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      form1: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        userPhone: undefined,
        orderSn: undefined,
        overStatus: undefined,
        firstApply: undefined,
        twoExamineBy: undefined,
        applyLog: undefined,
        isFollowUp: undefined,
        channel: undefined,
        status: '2',
      },
      applyLogList: [
        {
          value: '0',
          label: '否'
        },
        {
          value: '1',
          label: '是'
        },
      ],
      isFollowUpList: [
        {
          value: '0',
          label: '否'
        },
        {
          value: '2',
          label: '是'
        },
      ],
      overStatusList: [
        {
          value: '0',
          label: '结清未逾期'
        },
        {
          value: '1',
          label: '否'
        },
      ],
      applyStatusList: [
        {
          id: '0',
          name: '首借'
        },
        {
          id: '2',
          name: '复借'
        }
      ],
      // 表单校验
      rules: {
        remark: [
          {required: true, message: "备注不能为空", trigger: "blur"},
        ],
        productId: [
          {required: true, message: "请选择金额", trigger: "blur"},
        ]
      }
    };
  },
  watch: {},
  created() {
    if (this.$route.query && this.$route.query.pageNum) {
      this.queryParams.pageNum = parseInt(this.$route.query.pageNum);
      this.queryParams.pageSize = parseInt(this.$route.query.pageSize);
      this.queryParams.userName = this.$route.query.userName;
      this.queryParams.userPhone = this.$route.query.userPhone;
      this.queryParams.orderSn = this.$route.query.orderSn;
      this.queryParams.overStatus = this.$route.query.overStatus;
      this.queryParams.firstApply = this.$route.query.firstApply;
      this.queryParams.applyLog = this.$route.query.applyLog;
      this.queryParams.towExamineBy = this.$route.query.towExamineBy;
    }
    this.getList();
    this.getChannelList();
    this.getListExamineUser();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if (this.companyId) {
        this.queryParams.companyId = this.companyId
      }
      listTwoApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    getListExamineUser() {
      listExamineUser().then(response => {
          this.userList = response.data;
        }
      );
    },

    getChannelList() {
      channelList().then(response => {
        this.channelList = response.data;
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        frozenDay: undefined,
        remark: undefined,
        ids: undefined
      };
      this.resetForm("form");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 审核 */
    handleExamine(row) {
      this.reset();
      this.form.applyId = row.id;
      listProduct().then(response => {
        this.applyProductList = response.data
      })
      this.form1Show = false;
      this.open = true;
    },

    examinePassHandler() {
      this.form.status = '1';
      this.$refs["form"].validate(valid => {
        examineTwoApply(this.form).then(response => {
          this.$modal.msgSuccess("操作成功");
          this.open = false;
          this.getList();
        })
      })
    },

    examineFailHandler() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.ids = this.ids.join(",");
          examineTwoApply(this.form).then(response => {
            this.$modal.msgSuccess("success");
            this.open = false;
            this.getList();
          })
        }
      })
    },

    tplSelectChange(value) {
      this.applyProductList.forEach(item => {
        if (item.id == value) {
          this.form1 = item;
        }
      })
      this.form1Show = true;
    },

    handleUpdateIds() {
      this.open = true;
    },

    /** 提交按钮 */
    submitForm: function () {
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != undefined) {
      //       updateIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('closed')).then(function () {
        return closeApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("success");
      }).catch(() => {
      });
    },

    // handleMoney(){
    //   this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
    // }
  }
};
</script>
